import { IConfig } from 'umi-types';
import slash from 'slash2';
import proxy from './proxy';
import { device, statistics, callpoliceManage, system, journalManage } from './routesConfig';

const path = require('path');

// ref: https://umijs.org/config/
const config: IConfig = {
  treeShaking: true,
  alias: {
    '@': path.resolve(path.resolve(__dirname, 'src')),
  },
  define: {
    "process.env.MAIN_TITLE": '陕西省渭南戒毒所物联网管控平台',
    "process.env.WS_BASE_API": 'ws://192.168.0.110:9999/iotApi',
    "process.env.BASE_API": '/baseApi',
    'process.env.BASE_UPLOAD_URL': 'http://192.168.0.110:9999/iotApi',
  },
  routes: [
    {
      path: '/login',
      component: './login',
      Routes: ['src/AuthRouter'],
    },
    {
      path: '/',
      redirect: '/home',
    },
    {
      path: '/',
      component: '../layouts',
      routes: [
        { path: '/home', component: './home', Routes: ['src/AuthRouter'] }, // 首页
        ...device,
        ...statistics,
        ...callpoliceManage,
        ...system,
        ...journalManage,
      ],
    },
  ],
  plugins: [
    // ref: https://umijs.org/plugin/umi-plugin-react.html
    [
      'umi-plugin-react',
      {
        antd: true,
        dva: {
          immer: true,
        },
        dynamicImport: { webpackChunkName: true },
        title: '物联网管控平台',
        dll: true,
        routes: {
          exclude: [
            /models\//,
            /services\//,
            /model\.(t|j)sx?$/,
            /service\.(t|j)sx?$/,
            /components\//,
          ],
        },
      },
    ],
  ],
  cssLoaderOptions: {
    modules: true,
    getLocalIdent: (
      context: {
        resourcePath: string;
      },
      _: string,
      localName: string,
    ) => {
      if (
        context.resourcePath.includes('node_modules') ||
        context.resourcePath.includes('ant.design.pro.less') ||
        context.resourcePath.includes('global.less')
      ) {
        return localName;
      }

      const match = context.resourcePath.match(/src(.*)/);

      if (match && match[1]) {
        const antdProPath = match[1].replace('.less', '');
        const arr = slash(antdProPath)
          .split('/')
          .map((a: string) => a.replace(/([A-Z])/g, '-$1'))
          .map((a: string) => a.toLowerCase());
        return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
      }

      return localName;
    },
  },
  proxy,
};

export default config;
